package com.masterrabbit.service;

import com.masterrabbit.domain.Reviews;
import com.masterrabbit.domain.User;
import com.masterrabbit.mapper.ImagesMapper;
import com.masterrabbit.mapper.ReviewsMapper;
import com.masterrabbit.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReviewService {
    @Autowired
    private ReviewsMapper reviewsMapper;
    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private UserMapper userMapper;

    public List<Reviews> searchReview(Integer merchantId) {
        List<Reviews> reviews = reviewsMapper.searchReview(merchantId);
        if (reviews != null) {
            for (Reviews review : reviews) {
                review.setImages(imagesMapper.selectByProductId(review.getImageId()));
                User user = userMapper.selectByPrimaryKey(review.getUserId());
                //将姓名隐藏*与字数一致
                user.setNickName(hideNickname(user.getNickName()));
                review.setUser(user);
            }
        }
        return reviews;
    }
    public static String hideNickname(String nickname) {
        if (nickname == null || nickname.isEmpty()) {
            return nickname;
        }
        // 获取昵称的长度
        int length = nickname.length();
        // 构造隐藏后的昵称，保留首字母，其余用星号代替
        StringBuilder hiddenNickname = new StringBuilder();
        hiddenNickname.append(nickname.charAt(0)); // 添加首字母
        for (int i = 1; i < length; i++) {
            hiddenNickname.append("*");
        }
        return hiddenNickname.toString();
    }
}
